package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-03-12
 */
@Service

public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Override
    public ResponseResult listBystatus(AuthDto dto) {
        //进行分页查询
        //构建分页条件
        LambdaQueryWrapper<ApUserRealname> query=new LambdaQueryWrapper<>();
        if (dto.getStatus()!=null) {
            query.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        //构建查询条件
        IPage<ApUserRealname> page=new Page<>(dto.getPage(),dto.getSize());
        //根据状态进行查询
        IPage<ApUserRealname> iPage = this.page(page, query);
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), iPage.getTotal(),iPage.getRecords());
        return result;
    }

    @Autowired
    private IApUserService userService;
    @Autowired
    private MediaFeign mediaFeign;
    @Autowired
    private ArticleFeign articleFeign;
    //审核
    @Override
    //@GlobalTransactional
    public ResponseResult auth(AuthDto dto, int type) {
        //根据id查询认证记录
        ApUserRealname apUserRealname = this.getById(dto.getId());
        //审核失败 修改认证表状态为2 并记录失败原因
        if (type==0) {
            apUserRealname.setStatus(2);
            apUserRealname.setReason(dto.getReason());
            this.updateById(apUserRealname);
            return ResponseResult.okResult();
        }
        //查询用户注册表
        ApUser apUser = userService.getById(apUserRealname.getUserId());
        //审核通过\
        //更新认证表装态
        apUserRealname.setStatus(9);
        this.updateById(apUserRealname);
        //创建自媒体账号
        WmUser wmUser = createdWmUser(apUser);
        int i = 1 / 0;
        //创建文章作者
        if (wmUser!=null) {
            ApAuthor apAuthor = createAuthor(apUser, wmUser);
            if (apAuthor!=null) {
                //更新自媒体账号
                wmUser.setApAuthorId(apAuthor.getId());
                mediaFeign.updateWmUser(wmUser);
                //更新ap_user中的flag
                apUser.setFlag(1);
                userService.updateById(apUser);
            }
        }
        return ResponseResult.okResult();
    }
    //创建文章作者
    private ApAuthor createAuthor(ApUser apUser, WmUser wmUser) {
        ApAuthor author= new ApAuthor();
        author.setName(apUser.getName());
        author.setUserId(apUser.getId());
        author.setWmUserId(wmUser.getId());
        author.setType(2);
        ResponseResult<ApAuthor> apAuthorResponseResult = articleFeign.saveApAuthor(author);
        if (apAuthorResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            ApAuthor apAuthor = apAuthorResponseResult.getData();
            return apAuthor;
        }
        return null;
    }
    //创建自媒体用户
    private WmUser createdWmUser(ApUser apUser) {
        //远程调用自媒体服务创建用户
        WmUser wmUser = new WmUser();
        BeanUtils.copyProperties(apUser, wmUser);
        wmUser.setId(null);
        wmUser.setApUserId(apUser.getId());
        wmUser.setNickname(apUser.getName());
        wmUser.setStatus(9);
        wmUser.setType(0);
        ResponseResult<WmUser> wmUserResponseResult = mediaFeign.saveWmUser(wmUser);
        if (wmUserResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            WmUser user = wmUserResponseResult.getData();
            return user;
        }

        return null;
    }

}
